<template>
  <el-form
    :model="form"
    status-icon
    :rules="rules"
    ref="form"
    label-width="100px"
    class="login-container"
  >
    <h3>系统登录</h3>
    <el-form-item
      label="用户名"
      label-width="80px"
      prop="username"
      class="username"
    >
      <el-input
        type="input"
        auto-complete="off"
        v-model="form.username"
        placeholder="请输入用户名"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="密码"
      label-width="80px"
      prop="password"
      class="password"
    >
      <el-input
        type="password"
        auto-complete="off"
        v-model="form.password"
        placeholder="请输入密码"
        @keyup.enter.native="login"
      ></el-input>
    </el-form-item>
    <el-form-item class="login-submit"
      ><el-button type="primary" @click="login" class="login-submit"
        >登录</el-button
      ></el-form-item
    >
  </el-form>
</template>

<script>
// import Mock from "mockjs";
// import Cookie from 'js-cookie'
import { getMenu } from "../api/data";
export default {
  name: "Login",
  data() {
    return {
      form: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            message: "用户名长度不能小于3位",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    login() {

      // const token = Mock.Random.guid()
      // Cookie.set('token',token)

      getMenu(this.form).then(({ data: res }) => {
        console.log(res);
        if (res.code === 20000) {
          this.$store.commit("clearMenu");
          this.$store.commit("setMenu", res.data.menu);
          this.$store.commit("setToken", res.data.token);
          this.$store.commit("addMenu", this.$router);
          this.$store.commit("addMenu", this.$router);
          this.$router.push({ name: "home" });
        } else {
          this.$message.warning(res.data.message);
        }
      });

      // const token = Mock.random.guid();
      // this.$store.commit("setToken", token);
      // this.$router.push({ name: "home" });
    },
  },
};
</script>

<style>
.login-container {
  border-radius: 15px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  padding: 10px 35px;
  box-shadow: 0 0 25px #cac6c6;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
h3 {
  text-align: center;
}
.login-submit {
  margin: auto;
}
</style>